package org.example.controller;


import org.example.pojo.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class AyUserController {

    @Autowired
    private RestTemplate restTemplate;

//        private static final String REST_URL_PREFIX = "http://localhost:8001";
    private static final String REST_URL_PREFIX = "http://SPRINGCLOUD-PROVIDER-USER";

    @RequestMapping("/consumer/user/get/{id}")
    public SysUser getUser(@PathVariable Integer id) {
//        SysUser sysUser = restTemplate.getForEntity(REST_URL_PREFIX + "/user/get/" + id, SysUser.class).getBody();
        return restTemplate.getForObject(REST_URL_PREFIX + "/user/get/" + id, SysUser.class);
    }

    @GetMapping("/consumer/user/add")
    public boolean addUser(SysUser sysUser) {
//        restTemplate.postForEntity()
        return restTemplate.postForObject(REST_URL_PREFIX + "/user/add", sysUser, Boolean.class);
    }

    @GetMapping("/consumer/user/list")
    public List<SysUser> queryAll() {
        return restTemplate.getForObject(REST_URL_PREFIX + "/user/list", List.class);
    }

}
